package com.clstu.regexp;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 演示正则表达式的 捕获分组
 */
public class RegExp04 {
    public static void main(String[] args) {
        String content = "4567jdfljb4568djsdlfj8520dljf;ad9532";
////        非命名分组
//        String regStr = "(\\d\\d)(\\d)(\\d)";
        //命名分组
        String regStr = "(?<g1>\\d\\d)(?<g2>\\d)(?<g3>\\d)";
        Pattern pattern = Pattern.compile(regStr);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()){
            System.out.println("找到: "+matcher.group(0));
            System.out.println("分组1: "+matcher.group(1));//通过组号来获取
            System.out.println("分组[命名分组]1: "+matcher.group("g1"));//也可以通过组名来获取
            System.out.println("分钟2: "+matcher.group(2));
            System.out.println("分组[命名分组]2: "+matcher.group("g2"));
            System.out.println("分钟3: "+matcher.group(3));
            System.out.println("分组[命名分组]3: "+matcher.group("g3"));
        }
    }
}
